#include "peripheral.h"
#include "mc_interface.h"
#include "hardware_config.h"
#include "task_scheduler.h"

LEDPWM PWMDuty = {.Start=0,/*.ONOFF=0,*/.LedOFF=1,.FanOFF=0};

WIRELESS_REC WrilessRec;
WIRELESS_DAT WrilessDat = {.addr = 0, .cmd = 0, .crc = 0, .num = 0, .numLast = 0xff};

BEEP Beep = {.cnt = BEEP_ON_TIME, .status = 1};
ON_OFF_TIME OnOffTime = {.status = 0};

uint8_t crc=0;
uint8_t FAN_Shake = 0; 
//uint16_t FAN_Cmd = NO_COMMAND;
uint16_t pwmincr=0,pwmdecr=0;
uint16_t pwmk1k2=0;

uint32_t uwAddress = 0;
uint32_t readData = 0;
uint8_t Flagkey=0,ModeKey=0;
uint8_t FlagAC=0;
uint8_t FlagACOn=0;
uint8_t FlagSleep=0;
uint16_t SleepCnt=0;
 uint32_t CodeData[5];
uint8_t FlashSet;
/*void BeepTest(void)
{
    RELAY_PORT->BIT_SET_RST = RELAY_PIN;
}
*/

//-------------------------------------------------------------
void FlashWrite(uint32_t writeBuf)
{
		FLASH_Unlock();
		FLASH_ErasePage(FLASH_OPERATE_ADDR);
		FLASH_ProgramWord(FLASH_OPERATE_ADDR, writeBuf); //addr+4
		FLASH_Lock();
}

//-------------------------------------------------------------
void FlashRead(void)
{
	uint32_t data=0;
	
	FLASH_Unlock();
	data=FLASH_ReadWord(FLASH_OPERATE_ADDR);
	FLASH_Lock();
	
	if(data<5)
	{
		ModeKey=data;
	}
}

//-------------------------------------------------------------
void LED_PWM_Init(void)
{
	GPIO_InitTypeStruct CCT_GPIO_InitStruct;
	CCT_TimeBaseInitTypeStruct CCT_TimeBaseInitStruct;
	
	/* Enable clock */
  RCC_Unlock();
  RCC_AHBPeriphClockCmd(RCC_AHB_PERIPH_GPIO, ENABLE);
  RCC_APBPeriphClockCmd(RCC_APB_PERIPH_CCT0, ENABLE);
  RCC_Lock();
	
	GPIO_StructInit(&CCT_GPIO_InitStruct);
	CCT_GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INPUT; //
	CCT_GPIO_InitStruct.GPIO_Speed = GPIO_SPEED_HIGH;
	CCT_GPIO_InitStruct.GPIO_OType = GPIO_OTYPE_OD;  	
	CCT_GPIO_InitStruct.GPIO_PuPd = GPIO_PUPD_DOWN; // 下拉
	CCT_GPIO_InitStruct.GPIO_Pin = GPIO_PIN_13 | GPIO_PIN_14;
	GPIO_Init(GPIOA, &CCT_GPIO_InitStruct);
	
	CCT_GPIO_InitStruct.GPIO_Pin = AC_PIN;   //掉电检测
	GPIO_Init(AC_PORT, &CCT_GPIO_InitStruct);
	
	/* Config CCT0 GPIO */
  CCT_GPIO_InitStruct.GPIO_Mode = GPIO_MODE_ALT_FUNC;
  CCT_GPIO_InitStruct.GPIO_Speed = GPIO_SPEED_HIGH;
  CCT_GPIO_InitStruct.GPIO_OType = GPIO_OTYPE_PP;
  CCT_GPIO_InitStruct.GPIO_PuPd = GPIO_PUPD_NOPULL;
  CCT_GPIO_InitStruct.GPIO_Pin = GPIO_PIN_5;
  GPIO_Init(GPIOA, &CCT_GPIO_InitStruct);
  GPIO_AltFuncConfig(GPIOA, GPIO_PIN_5, GPIO_ALT_FUNC_2);
	
	CCT_GPIO_InitStruct.GPIO_Pin = GPIO_PIN_4;
  GPIO_Init(GPIOB, &CCT_GPIO_InitStruct);
  GPIO_AltFuncConfig(GPIOB, GPIO_PIN_4, GPIO_ALT_FUNC_1);
	
  /* Config LED GPIO */
  CCT_DeInit(CCT0);
//	CCT_DeInit(CCT1);
  /* Config CCT0 */
  CCT_TimeBaseStructInit(&CCT_TimeBaseInitStruct);
  CCT_TimeBaseInitStruct.CCT_CounterCycle = CCT_COUNTER_CYCLE_PERIOD;
  CCT_TimeBaseInitStruct.CCT_ClockDivision = CCT_CLK_DIV16;
  CCT_TimeBaseInitStruct.CCT_ReloadValue = 1500 - 1;  //1ms
	CCT_TimeBaseInit(CCT0, &CCT_TimeBaseInitStruct);
  CCT_Cmd(CCT0, ENABLE);
	
	//CCT_TimeBaseInit(CCT1, &CCT_TimeBaseInitStruct);
 // CCT_Cmd(CCT1, ENABLE);
	
  /* Config CCT0 output compare */
  CCT_OCInitTypeStruct CCT_OCInitStruct;
  CCT_OCStructInit(&CCT_OCInitStruct);
  CCT_OCInitStruct.CCT_OCPulse = 0;
  CCT_OCInitStruct.CCT_OCPolarity = CCT_OC_POLARITY_HIGH;
  CCT_OC1Init(CCT0, &CCT_OCInitStruct);
  CCT_CH1Cmd(CCT0, ENABLE);
	
	CCT_SetCompare1(CCT0,0);
	//CCT_SetCompare0(CCT1,0);
 // CCT_OC0Init(CCT1, &CCT_OCInitStruct);
 // CCT_CH0Cmd(CCT1, ENABLE);
	
	//PWMDuty.ONOFF=0;
	PWMDuty.FanOFF=0;
	PWMDuty.LedOFF=0;
	pwmincr=0,pwmdecr=0;
	PWMDuty.Start=0;
	PWMDuty.Wdata=0;
	PWMDuty.Ydata=0;
	PWMDuty.WdataK=0;
	PWMDuty.YdataK=0;
	PWMDuty.startY=0;
	PWMDuty.startW=0;
	
	//FlashRead();
	ModeKey=0;
	FlagAC=1;
}

//-----------------------------------------------------
void Peripheral_Init(void)
{
    GPIO_InitTypeStruct struGPIO_Init;
    TIM_InitTypeStruct struTIM_Init;
    
    RCC_Unlock();
    RCC_AHBPeriphClockCmd(RCC_AHB_PERIPH_GPIO, ENABLE);
    RCC_APBPeriphClockCmd(RCC_APB_PERIPH_TIM1, ENABLE);
//	RCC_APBPeriphClockCmd(RCC_APB_PERIPH_ACMP, ENABLE);
    RCC_Lock();
    
    TIM_DeInit(TIM1);
    TIM_StructInit(&struTIM_Init);
    
    struTIM_Init.TIM_CounterCycle = TIM_COUNTER_CYCLE_SINGLE;
    struTIM_Init.TIM_IntState = ENABLE;
    struTIM_Init.TIM_ReloadValue = 240000;  //定时 5ms
    TIM_Init(TIM1, &struTIM_Init);
    
    TIM_IntCmd(TIM1, ENABLE);
    
    //继电器
    //GPIO_StructInit(&struGPIO_Init);
    //struGPIO_Init.GPIO_Mode = GPIO_MODE_OUTPUT; //
    //struGPIO_Init.GPIO_Speed = GPIO_SPEED_HIGH;
    //struGPIO_Init.GPIO_OType = GPIO_OTYPE_PP;   // 推挽
    //struGPIO_Init.GPIO_PuPd = GPIO_PUPD_NOPULL; // 无上下拉
    
    //struGPIO_Init.GPIO_Pin = RELAY_PIN;
    //GPIO_Init(RELAY_PORT, &struGPIO_Init);
    
    //蜂鸣器
    //struGPIO_Init.GPIO_Pin = BEEP_PIN;
    //GPIO_Init(BEEP_PORT, &struGPIO_Init);
    
    //Beep.status = 1;
		//Beep.cnt = BEEP_ON_TIME;
    
    
    //遥控接受引脚
       //配置引脚为中断模式，边沿触发
    GPIO_StructInit(&struGPIO_Init);
    struGPIO_Init.GPIO_Mode = GPIO_MODE_INPUT; //
    struGPIO_Init.GPIO_Speed = GPIO_SPEED_HIGH;
    struGPIO_Init.GPIO_OType = GPIO_OTYPE_PP;   // 推挽
    struGPIO_Init.GPIO_PuPd = GPIO_PUPD_NOPULL; // 无上下拉
    
    struGPIO_Init.GPIO_Pin = WIRELESS_PIN;
    GPIO_Init(WIRELESS_PORT, &struGPIO_Init);
    
    
    GPIO_IntConfig(WIRELESS_PORT, WIRELESS_PIN, GPIO_INT_RISING_FALLING);
    GPIO_IntEnable(WIRELESS_PORT, WIRELESS_PIN);
    
    NVIC_SetPriority(WIRELESS_IRQn, 2);
    NVIC_EnableIRQ(WIRELESS_IRQn);
    NVIC_SetPriority(TIM1_IRQn, 2);
    NVIC_EnableIRQ(TIM1_IRQn);	
		
		//----------------------------
	//	LED_PWM_Init();
		
}

//定时中断

void TIM1_IRQHandler(void)
{
    if(TIM_GetIntFlagStatus(TIM1) == SET)
    {
        TIM_ClearIntFlag(TIM1);
        WrilessRec.RecComplete = 1;
    }
}

uint32_t TimCnt = 0;
void WIRELESS_IRQHandler(void)
{

    if(GPIO_GetIntFlagStatus(WIRELESS_PORT, WIRELESS_PIN) == SET)
    {
        GPIO_ClearIntFlag(WIRELESS_PORT, WIRELESS_PIN);
        

        if(WrilessRec.RecComplete == 1)
            return;
        TimCnt = TIM1->ARR - TIM1->CNT;
        TIM1->CNT = TIM1->ARR;
              
        //上升沿
        if(GPIO_ReadInputBit(WIRELESS_PORT, WIRELESS_PIN) == BIT_SET)
        {
            if(WrilessRec.RecBitCnt == 0)
            {
                TIM_Cmd(TIM1, ENABLE);
            }
        }
        //下降沿
        else
        {
            if(WrilessRec.RecComplete == 0)
            {
                // 数据为0,否则为1
                if(TimCnt < TIMER1_DETA_MID1 && TimCnt > TIMER1_DETA_MIN) //IR,0==250,1==750
                {
                        WrilessRec.RecBitBuf[WrilessRec.RecBitCnt++] = 0;
                }
                else if(TimCnt > TIMER1_DETA_MID2 && TimCnt < TIMER1_DETA_MAX)
                {
                        WrilessRec.RecBitBuf[WrilessRec.RecBitCnt++] = 1;
                }
                else
                {
                        WrilessRec.RecError = 1;
                        WrilessRec.RecBitCnt = 0;
                }
                //------------------------------------------------------
                if(WrilessRec.RecBitCnt >= 32) //33bit
                {
                    WrilessRec.RecBitCnt=0;
                    WrilessRec.RecComplete = 1;
                }
            }
        }
        
    }
}
//*********************************************************
void WrilessRec_Handler(void)
{
		uint8_t i,FAN_Cmd;
    uint32_t RecData = 0;
		if(FlagACOn==0)
		{
			SleepMcu();
			LED_ACONOFF();
			//------------------------------------------
			if(WrilessRec.RecComplete == 1)
			{
					WrilessRec.RecComplete = 0;
				WrilessDat.addr = 0;
				//addr
				for(uint8_t i = 0;i < 20;i++)
				{
						WrilessDat.addr <<= 1;
						WrilessDat.addr |= WrilessRec.RecBitBuf[i];
						RecData <<= 1;
						RecData |= WrilessRec.RecBitBuf[i];
				}
				
				WrilessDat.cmd = 0;
				//cmd
				for(uint8_t i = 0;i < 5;i++)
				{
						WrilessDat.cmd <<= 1;
						WrilessDat.cmd |= WrilessRec.RecBitBuf[20+i];
						RecData <<= 1;
						RecData |= WrilessRec.RecBitBuf[20+i];
				}
				
				WrilessDat.num = 0;
				//序号
				for(uint8_t i = 0;i < 3;i++)
				{
						WrilessDat.num <<= 1;
						WrilessDat.num |= WrilessRec.RecBitBuf[25+i];
						RecData <<= 1;
						RecData |= WrilessRec.RecBitBuf[25+i];
				}
				
				WrilessDat.crc = 0;
				//效验码
				for(uint8_t i = 0;i < 4;i++)
				{
						WrilessDat.crc <<= 1;
						WrilessDat.crc |= WrilessRec.RecBitBuf[28+i];
//                RecData <<= 1;
//                RecData |= WrilessRec.RecBitBuf[28+i];
				}
				
				//CRC
				crc = 0;
				for(uint8_t i = 0;i < 8;i++)
				{
						crc ^= RecData;
						RecData >>= 4;
				}
				crc = (crc ^ 0x00 ^ 0x0a) & 0x0f;
					//---------------------------------------------------------
				if(crc == WrilessDat.crc )
				{
					if((WrilessDat.addr == CodeData[0] || WrilessDat.addr == CodeData[1]\
						||WrilessDat.addr == CodeData[2] ||WrilessDat.addr == CodeData[3]||WrilessDat.addr == CodeData[4]) \
						 && WrilessDat.num == WrilessDat.numLast && (WrilessRec.RecOnce == 1))
					{
                        switch(WrilessDat.cmd)
                        {
                            case REMOTE_CODE_FAN_L1:   //一档
																#if(CONTROL_MODE==0)
																		SignalChain.hVSPointer = LEVEL1_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL1_POWER_CMD;
																#endif
														    Beep.status = 1;
														    FAN_Cmd = CMD_START;
                            break;
                            
                            case REMOTE_CODE_FAN_L2:   //二档
																#if(CONTROL_MODE==0)
																		SignalChain.hVSPointer = LEVEL2_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL2_POWER_CMD;
																#endif
														    Beep.status = 1;
														    FAN_Cmd = CMD_START;
                            break;
                            
                            case REMOTE_CODE_FAN_L3:   //三档
																#if(CONTROL_MODE==0)
																		SignalChain.hVSPointer = LEVEL3_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL3_POWER_CMD;
																#endif
														    Beep.status = 1;
														    FAN_Cmd = CMD_START;
                            break;
                            
                            case REMOTE_CODE_FAN_L4:    //四档
																#if(CONTROL_MODE==0)
																		SignalChain.hVSPointer = LEVEL4_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL4_POWER_CMD;
																#endif
														    Beep.status = 1;
														
														    FAN_Cmd = CMD_START;
														    
                            break;                 
                            case REMOTE_CODE_FAN_L5:   //五档
																#if(CONTROL_MODE==0)
																		SignalChain.hVSPointer = LEVEL5_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL5_POWER_CMD;
																#endif
														    Beep.status = 1;
														    
														   FAN_Cmd = CMD_START;
														
                            break;               
                            case REMOTE_CODE_FAN_L6:   //六档
																#if(CONTROL_MODE==0)
																		SignalChain.hVSPointer = LEVEL6_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL6_POWER_CMD;
																#endif
														    Beep.status = 1;
														    
														   FAN_Cmd = CMD_START;
                            break;
                            
                                                
                            case 0x1f:  //风扇关
																	OnOffTime.status = 0;
																	FAN_Cmd = CMD_STOP;
																	//Beep.status = 0;
														      Beep.status = 1;
                            break;
                                                
                            case 0x01:   //全部关闭
																	OnOffTime.status = 0;
//																	RELAY_OFF;
//																	FAN_Shake = 0;
//																	ZiRanFan=0;
//																	FlagSwitchLed=0;
																	FAN_Cmd = CMD_STOP;
														      Beep.status = 1;
                            break;
                                                        
                            case 0x06:   //开灯
															if(FAN_Shake==0)
															{
//																FAN_Shake=1;RELAY_ON;
//																FlagSwitchLed=0;
														    Beep.status = 1;
															}
															else
															{
//																FAN_Shake=0;RELAY_OFF;
//																FlagSwitchLed=0;
														    Beep.status = 1;
															}
															break;
                            break;
                                                
                            case REMOTE_CODE_FAN_CW:  //正转
															if(StateMachine !=  IDLE && StateMachine != STOP)
																{
																	#if(CONTROL_MODE==0)
																			SignalChain.hVSPointer = LEVEL3_SPEED_CMD;
																	#else
																			SignalChain.hPowref = LEVEL3_POWER_CMD;
																	#endif
																	FOC.PWM.bDirShadow = DEFAULT_MOTOR_DIRECTION;
																	FAN_Cmd = CMD_START;
																}
															
														    Beep.status = 1;
															
																	
                            break;
                                                
                            case REMOTE_CODE_FAN_CCW:   //反转
															if(StateMachine !=  IDLE && StateMachine != STOP)
																{
																	#if(CONTROL_MODE==0)
																			SignalChain.hVSPointer = LEVEL3_SPEED_CMD;
																	#else
																			SignalChain.hPowref = LEVEL3_POWER_CMD;
																	#endif
																	FOC.PWM.bDirShadow = DEFAULT_MOTOR_DIRECTION;
																	FAN_Cmd = CMD_START;
																}
														    Beep.status = 1;
																
                            break;
														
														case 0x09:   //反转
															if(StateMachine !=  IDLE && StateMachine != STOP)
																{
                                #if(CONTROL_MODE==0)
																		SignalChain.hVSPointer = LEVEL3_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL3_POWER_CMD;
																#endif
																FOC.PWM.bDirShadow = !DEFAULT_MOTOR_DIRECTION;
																FAN_Cmd = CMD_START;
																}
															
														    Beep.status = 1;
																
                            break;
														case 0x16:   //自然风
                            case REMOTE_CODE_FAN_WIND:   //自然风
//																ZiRanFan=1;ZiRanCnt=0;
//																ZiRanMode=0;FlagZIRan=0;
																#if(CONTROL_MODE==0)
																SignalChain.hVSPointer = LEVEL1_SPEED_CMD;
																#else
																		SignalChain.hPowref = LEVEL1_POWER_CMD;
																#endif
																FOC.PWM.bDirShadow = DEFAULT_MOTOR_DIRECTION;
																FAN_Cmd = CMD_START;
														    Beep.status = 1;
																break;
                            default:
                                break;
                        }
						
						/***************************************/
						Command = FAN_Cmd;
					}
					if(FlashSet==0)
					{
						if(WrilessDat.numLast == WrilessDat.num&&WrilessDat.cmd==0x1F)
						{
							FlashSet = 1;
							
							CodeData[4]=CodeData[3];
							CodeData[3]=CodeData[2];
							CodeData[2]=CodeData[1];
							CodeData[1]=CodeData[0];
							CodeData[0]=WrilessDat.addr;
							
//							FlashWriteBuf(); //记忆
							
							Beep.status = 1;
						}
						
						if(WrilessDat.numLast == WrilessDat.num&&WrilessDat.cmd==0x01)
						{
//							FlashClear++;
//							if(FlashClear>30)
//							{
//								FlashSet = 1;
//							
//								CodeData[4]=0xFFFFFFFF;
//								CodeData[3]=0xFFFFFFFF;
//								CodeData[2]=0xFFFFFFFF;
//								CodeData[1]=0xFFFFFFFF;
//								CodeData[0]=0xFFFFFFFF;
//							
//							  FlashWriteBuf(); //记忆
								
						//		Beep.status = 1;
							}
						}
					}
					if(WrilessDat.numLast == WrilessDat.num)
					{
						if(WrilessRec.RecOnce < 3)
							WrilessRec.RecOnce ++;
					}else{WrilessRec.RecOnce = 0;}
//				if(WrilessDat.num != WrilessDat.numLast)
//				{
//					WrilessRec.RecOnce = 0;
//				}
					WrilessDat.numLast = WrilessDat.num;
					
				}
			}
}

//----------------------------------------------
void LED_OFF(void)
{
	FlagAC=0;
	PWMDuty.LedOFF = 0;
	PWMDuty.Start=6;
	PWMDuty.startY=PWMDuty.YdataK;
	PWMDuty.startW=PWMDuty.WdataK;
	PWMDuty.YdataK=0;
	PWMDuty.WdataK=0;
	pwmincr=0;pwmdecr=0;
}
//----------------------------------------------
void LEDIncrDecr(void)
{
	if(PWMDuty.LedOFF==0)
	{
		PWMDuty.LedOFF=1;
		PWMDuty.Start=2;
		PWMDuty.startY=0;
		PWMDuty.startW=0;
		PWMDuty.Cnt=0;
	}
	else 
	{
		PWMDuty.Start=7;
		PWMDuty.Cnt=0;
	}
}

//*********************************************************
void LED_ACONOFF(void)
{
		if(FlagAC==1)
		{
			FlagAC=0;
			if(ModeKey==0)
			{
				PWMDuty.Wdata=1500;
				PWMDuty.Ydata=0;
				LEDIncrDecr();
				
				if(PWMDuty.FanOFF==1)
				{
					PWMDuty.FanOFF=0;
					OnOffTime.status = 0;
					FAN_Shake = 0;
					Command = CMD_STOP;
				}
			}
			else if(ModeKey==1)
			{
				PWMDuty.Wdata=1500;
				PWMDuty.Ydata=0;
				LEDIncrDecr();
				
				PWMDuty.FanOFF=1;
				Command = CMD_START;
			}
			else if(ModeKey==2)
			{
				PWMDuty.LedOFF=0;
				PWMDuty.Start=0;
				
				PWMDuty.FanOFF=1;
				Command = CMD_START;
			}
			else if(ModeKey==3)
			{
				PWMDuty.Wdata=0;
				PWMDuty.Ydata=1500;
				LEDIncrDecr();
				
				if(PWMDuty.FanOFF==1)
				{
					PWMDuty.FanOFF=0;
					OnOffTime.status = 0;
					FAN_Shake = 0;
					Command = CMD_STOP;
				}
			}
			else if(ModeKey==4)
			{
				PWMDuty.Wdata=750;
				PWMDuty.Ydata=750;
				LEDIncrDecr();
				
				if(PWMDuty.FanOFF==1)
				{
					PWMDuty.FanOFF=0;
					OnOffTime.status = 0;
					FAN_Shake = 0;
					Command = CMD_STOP;
				}
			}
	 }
}

//*****************************************************
void PWM_INCR(void)
{
	if(PWMDuty.Start == 1)
	{
			//------------------------------------------------
			if(ModeKey==4)
			{
				if(pwmincr>0) //++
				{
						if(PWMDuty.YdataK>0)
						{
							if(PWMDuty.YdataK<750)PWMDuty.YdataK++;
							else PWMDuty.YdataK=750;
						}
						//-----------------------------
						if(PWMDuty.WdataK>0)
						{
							if(PWMDuty.WdataK<750)PWMDuty.WdataK++;
							else PWMDuty.WdataK=750;
						}
						pwmincr--;
						if(pwmincr==0)
						{
							PWMDuty.Start=0;
						}
				}
			}
			else
			{
				if(pwmincr>0) //++
				{
						if(PWMDuty.YdataK>0)
						{
							if(PWMDuty.YdataK<1500)PWMDuty.YdataK++;
							else PWMDuty.YdataK=1500;
						}
						//-----------------------------
						if(PWMDuty.WdataK>0)
						{
							if(PWMDuty.WdataK<1500)PWMDuty.WdataK++;
							else PWMDuty.WdataK=1500;
						}
						pwmincr--;
						if(pwmincr==0)
						{
							PWMDuty.Start=0;
						}
				}
			}
			//-------------------------------------------------
			if(pwmdecr>0) //--
			{
					if(PWMDuty.YdataK>0)
					{
						if(PWMDuty.YdataK>150)PWMDuty.YdataK--;
						else PWMDuty.YdataK=150;
					}
					//-----------------------------
					if(PWMDuty.WdataK>0)
					{
						if(PWMDuty.WdataK>150)PWMDuty.WdataK--;
						else PWMDuty.WdataK=150;
					}
					pwmdecr--;
					if(pwmdecr==0)
					{	
						PWMDuty.Start=0;
					}
			}
			//CCT_SetCompare0(CCT1,PWMDuty.YdataK);
			CCT_SetCompare1(CCT0,PWMDuty.WdataK);
	 }
	 else if(PWMDuty.Start==2)
	 {
		 if(ModeKey==4)
		 {
				if(PWMDuty.Wdata>150)
				{
					if(PWMDuty.startW<750)PWMDuty.startW+=10;
					else PWMDuty.startW=750;
					CCT_SetCompare1(CCT0,PWMDuty.startW);
				}
				else if(PWMDuty.Wdata==150)
				{
					if(PWMDuty.startW<150)
					{
						PWMDuty.startW++;
						CCT_SetCompare1(CCT0,PWMDuty.startW);
					}
				}
				//---------------------------------------------
				if(PWMDuty.Ydata>150)
				{
					if(PWMDuty.startY<750)PWMDuty.startY+=10;
					else PWMDuty.startY=750;
				//	CCT_SetCompare0(CCT1,PWMDuty.startY);
				}
				else if(PWMDuty.Ydata==150)
				{
					if(PWMDuty.startY<150)
					{
						PWMDuty.startY++;
					//	CCT_SetCompare0(CCT1,PWMDuty.startY);
					}
				}
		 }
		 else
		 {
				if(PWMDuty.Wdata>150)
				{
					if(PWMDuty.startW<1500)PWMDuty.startW+=10;
					else PWMDuty.startW=1500;
					CCT_SetCompare1(CCT0,PWMDuty.startW);
				}
				else if(PWMDuty.Wdata==150)
				{
					if(PWMDuty.startW<150)
					{
						PWMDuty.startW++;
						CCT_SetCompare1(CCT0,PWMDuty.startW);
					}
				}
				//---------------------------------------------
				if(PWMDuty.Ydata>150)
				{
					if(PWMDuty.startY<1500)PWMDuty.startY+=10;
					else PWMDuty.startY=1500;
				//	CCT_SetCompare0(CCT1,PWMDuty.startY);
				}
				else if(PWMDuty.Ydata==150)
				{
					if(PWMDuty.startY<150)
					{
						PWMDuty.startY++;
						//CCT_SetCompare0(CCT1,PWMDuty.startY);
					}
				}
		 }
		 PWMDuty.Cnt++;
		 if(PWMDuty.Cnt>=160)
		 {
				PWMDuty.Cnt=0;
				PWMDuty.Start=0;
				//CCT_SetCompare0(CCT1,PWMDuty.Ydata);
				CCT_SetCompare1(CCT0,PWMDuty.Wdata);
				PWMDuty.YdataK=PWMDuty.Ydata;
				PWMDuty.WdataK=PWMDuty.Wdata;
		 }
	 }
	 else if(PWMDuty.Start==3)
	 {
		  if(pwmk1k2>0 && (PWMDuty.YdataK>0 && PWMDuty.WdataK<1500))
			{
					if(PWMDuty.YdataK>0)PWMDuty.YdataK--;
					else PWMDuty.YdataK=0;
					//-------------------------------------------------
					if(PWMDuty.WdataK<1500)PWMDuty.WdataK++;
					else PWMDuty.WdataK=1500;
				
					pwmk1k2--;
					if(pwmk1k2==0)
					{	
						PWMDuty.Start=0;
					}
			}
			else pwmk1k2=0;
			//CCT_SetCompare0(CCT1,PWMDuty.YdataK);
			CCT_SetCompare1(CCT0,PWMDuty.WdataK);
	 }
	 else if(PWMDuty.Start==4)
	 {
			if(pwmk1k2>0 && (PWMDuty.WdataK>0 && PWMDuty.YdataK<1500))
			{
					if(PWMDuty.WdataK>0)PWMDuty.WdataK--;
					else PWMDuty.WdataK=0;
					//----------------------------------------
					if(PWMDuty.YdataK<1500)PWMDuty.YdataK++;
					else PWMDuty.YdataK=1500;
					
					pwmk1k2--;
					if(pwmk1k2==0)
					{	
						PWMDuty.Start=0;
					}
			}
			else pwmk1k2=0;
			//CCT_SetCompare0(CCT1,PWMDuty.YdataK);
			CCT_SetCompare1(CCT0,PWMDuty.WdataK);
	 }
	 else if(PWMDuty.Start==5)
	 {
		 //----------------------------------------------------
		 if(PWMDuty.LedOFF==0)
		 {
			 if(PWMDuty.Cnt<25)PWMDuty.startW+=30;
			 else 
			 {
				  if(PWMDuty.startW>30)PWMDuty.startW-=30;
				  else PWMDuty.startW=0;
			 }
			 CCT_SetCompare1(CCT0,PWMDuty.startW);
			 PWMDuty.Cnt++;
			 if(PWMDuty.Cnt>=50)
			 {
				 PWMDuty.Cnt=0;
				 PWMDuty.Start =0;
				// CCT_SetCompare0(CCT1,0);
				 CCT_SetCompare1(CCT0,0);
			 }
		 }
		 else 
		 {
			 //-----------------------------------
			 if(PWMDuty.startY>=750)
			 {
				 if(PWMDuty.Cnt<25)
				 {
						if(PWMDuty.startY>780)PWMDuty.startY-=30;
				 }
				 else PWMDuty.startY+=30;
				// CCT_SetCompare0(CCT1,PWMDuty.startY);
			 }
			 else if(PWMDuty.startY>0)
			 {
				 if(PWMDuty.Cnt<720)PWMDuty.startY+=30;
				 else 
				 {
					 if(PWMDuty.startY>180)PWMDuty.startY-=30;
				 }
				// CCT_SetCompare0(CCT1,PWMDuty.startY);
			 }
			 
			 //-----------------------------------
			 if(PWMDuty.startW>750)
			 {
				 if(PWMDuty.Cnt<25)
				 {
						if(PWMDuty.startW>780)PWMDuty.startW-=30;
				 }
				 else PWMDuty.startW+=30;
				 CCT_SetCompare1(CCT0,PWMDuty.startW);
			 }
			 else if(PWMDuty.startW>0)
			 {
				 if(PWMDuty.Cnt<25)PWMDuty.startW+=30;
				 else 
				 {
						if(PWMDuty.startW>150)PWMDuty.startW-=30;
				 }
				 CCT_SetCompare1(CCT0,PWMDuty.startW);
			 }
			 PWMDuty.Cnt++;
			 if(PWMDuty.Cnt>=50)
			 {
				 PWMDuty.Cnt=0;
				 PWMDuty.Start =0;
				// CCT_SetCompare0(CCT1,PWMDuty.YdataK);
				 CCT_SetCompare1(CCT0,PWMDuty.WdataK);
			 }
		 }
	 }
	 else if(PWMDuty.Start==6)
	 {
		 //-----------------------------------
		 if(PWMDuty.startY>10)PWMDuty.startY-=10;
		 else PWMDuty.startY=0;
		 //CCT_SetCompare0(CCT1,PWMDuty.startY);
		 //-----------------------------------
		 if(PWMDuty.startW>10)PWMDuty.startW-=10;
		 else PWMDuty.startW=0;
		 CCT_SetCompare1(CCT0,PWMDuty.startW);
		 
		 if(PWMDuty.startW==0 && PWMDuty.startY==0)
		 {
			 PWMDuty.Start=0;
		 }
	 }
	 else if(PWMDuty.Start==7)
	 {
			if(PWMDuty.Wdata>0)
			{
					if(PWMDuty.WdataK<PWMDuty.Wdata)PWMDuty.WdataK+=10;
					else PWMDuty.WdataK=PWMDuty.Wdata;
					CCT_SetCompare1(CCT0,PWMDuty.WdataK);
			}
			else if(PWMDuty.Wdata==0)
			{
				if(PWMDuty.WdataK>10)PWMDuty.WdataK-=10;
				else PWMDuty.WdataK=0;
				CCT_SetCompare1(CCT0,PWMDuty.WdataK);
			}
			
			//---------------------------------------------
			if(PWMDuty.Ydata>0)
			{
				if(PWMDuty.YdataK<PWMDuty.Ydata)PWMDuty.YdataK+=10;
				else PWMDuty.YdataK=PWMDuty.Ydata;
			//	CCT_SetCompare0(CCT1,PWMDuty.YdataK);
			}
			else if(PWMDuty.Ydata==0)
			{
				if(PWMDuty.YdataK>10)PWMDuty.YdataK-=10;
				else PWMDuty.YdataK=0;
			//	CCT_SetCompare0(CCT1,PWMDuty.YdataK);
			}
			PWMDuty.Cnt++;
			if(PWMDuty.Cnt>=160)
			{
				PWMDuty.Cnt=0;
				PWMDuty.Start=0;
			}
	 }
}

//-------------------------------------------------------------
void KeyScan(void)
{
	static uint16_t Cnt1=0,Cnt2=0;
	
	if(GPIO_ReadInputBit(AC_PORT, AC_PIN)==BIT_SET && Flagkey==1)
	{
		Cnt2=0;
		if(++Cnt1>20) //ms
		{
			Cnt1=0;Flagkey=0;
			
			ModeKey++;
			if(ModeKey>=5)ModeKey=0;
			FlagAC=1;
			
			//uint32_t data=ModeKey;
			//FlashWrite(data);
		}
	}
	else if(GPIO_ReadInputBit(AC_PORT, AC_PIN)==BIT_RESET && Flagkey==0)
	{
		Cnt1=0;
		if(++Cnt2>350) //>40ms 
		{
			Cnt2=0;Flagkey=1;
			
			EPWM->OUT_CR = PWM_FLY; //关闭电机
			//CCT_SetCompare0(CCT1,0);
			CCT_SetCompare1(CCT0,0);
      StateFlag.bStop_done = false;
      Stop.bBrakeFlag = false;
	 (void)Epwm_init();
      (void)MCI_Init();
      Stop.hBrakeCnt = Stop.hBrakeTime;
			StateMachine = IDLE;
			
			LED_OFF();
			PWMDuty.Start=0;
			
			PWMDuty.FanOFF=0;
			OnOffTime.status = 0;
			FAN_Shake = 0;
			Command = CMD_STOP;
			FlagSleep=1;
			FlagACOn=1;
		}
	}
	else {Cnt1=0;Cnt2=0;}
}
//*****************************************************
void SleepMcu(void)
{
	if(FlagSleep)
	{
			FlagSleep=0;
			CCT_CH1Cmd(CCT0, DISABLE);
			//CCT_CH0Cmd(CCT1, DISABLE);
			EPWM->OUT_CR = PWM_FLY; //关闭电机
			GPIO_ResetBit(GPIOA, GPIO_PIN_5);
			GPIO_ResetBit(GPIOB, GPIO_PIN_4);
			
			/* Config rising interrupt */;
			GPIO_IntConfig(AC_PORT, AC_PIN, GPIO_INT_RISING);
			/* Enable interrupt */
			NVIC_SetPriority(AC_IRQn, 2);
			NVIC_EnableIRQ(AC_IRQn);
			GPIO_IntEnable(AC_PORT, AC_PIN);
			__NOP();__NOP();
			/* Enter stop mode */
			RCC_EnterLowPowerMode(RCC_LOW_POWER_MODE_STOP, RCC_LOW_POWER_ENTRY_WFI);
			__NOP();__NOP();
			/* Enable interrupt */
			NVIC_DisableIRQ(AC_IRQn);
			GPIO_IntDisable(AC_PORT, AC_PIN);
			CCT_CH1Cmd(CCT0, ENABLE);
			//CCT_CH0Cmd(CCT1, ENABLE);
			SystemTimer.pTimer[2]->bFlag = false;
            SystemTimer.pTimer[2]->hCounter = 400;
			FlagACOn=1;
	 }
}

//************************************************
void GPIOD_IRQHandler(void)
{
  if(RESET != GPIO_GetIntFlagStatus(AC_PORT,AC_PIN))
  {
    GPIO_ClearIntFlag(AC_PORT, AC_PIN);
	  
  }
}

